package gui;

import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.border.LineBorder;

import animation.Animator;

import java.awt.Color;
import java.awt.Dimension;
import mover.GameMover;
import mover.SimpleMover;

import data.Player;
import elements.GameCharacter;
import elements.GameElement;

import scaler.Scaler;
import net.miginfocom.swing.MigLayout;
import javax.swing.SwingConstants;
import java.awt.Font;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

public class PlayerInfoPane extends JPanel {
	private Player p;
	private GameCharacter gc;
	private boolean firstInit = true;
	private Animator scoreAnimator;
	private JLabel playerNameLabel;
	private Scaler scaler;
	private JLabel lblPlace;
	private JLabel lblScore;
	private static final int defScoreFontSize = 18;

	/**
	 * Create the panel.
	 */
	public PlayerInfoPane(Player p) {
		setOpaque(false);
		
		this.p = p;

		
		setFocusTraversalKeysEnabled(false);
		setLayout(new MigLayout("", "[grow 50,left][grow 50,right]", "[grow 66][grow 33]"));
		setBorder(new LineBorder(Color.DARK_GRAY, 1, true));
		
		playerNameLabel = new JLabel(p.getName());
		playerNameLabel.setForeground(GamePanel.FONT_COLOR);
		playerNameLabel.setBackground(GUI.BGGAME);
		add(playerNameLabel, "cell 0 0 2 1,alignx center,aligny center");
		
		lblPlace = new JLabel("1.");
		lblPlace.setBorder(null);
		lblPlace.setOpaque(true);
		lblPlace.setFont(new Font("Comic Sans MS", Font.BOLD, 11));
		lblPlace.setBackground(Color.ORANGE);
		lblPlace.setHorizontalAlignment(SwingConstants.CENTER);
		add(lblPlace, "cell 0 1,grow");
		
		lblScore = new JLabel("Score");
		lblScore.setHorizontalAlignment(SwingConstants.CENTER);
		lblScore.setForeground(GamePanel.FONT_COLOR);
		add(lblScore, "cell 1 1,grow");
		
		scaler = new Scaler(new Dimension(100, 40), lblScore);
		
}
	
	public void updateDisplay() {
		this.revalidate();
		if(p.getPoints() == 0){
			lblScore.setBorder(new LineBorder(Color.GRAY, 1, true));
			lblScore.setForeground(Color.GRAY);
		}
		else if(p.getPoints() < 0){
			lblScore.setBorder(new LineBorder(Color.RED, 1, true));
			lblScore.setForeground(Color.RED);
		}
		else{
			lblScore.setBorder(new LineBorder(Color.GREEN, 1, true));
			lblScore.setForeground(Color.GREEN);
		}
		lblScore.setFont(lblScore.getFont().deriveFont((float)scaler.calcY(defScoreFontSize)));
		lblScore.setText(p.getPoints() + "");
		playerNameLabel.setFont(lblScore.getFont().deriveFont((float)scaler.calcY(defScoreFontSize + 3)));

	}
	

	public void iHaveBuzzed(boolean buzzed){
		if(buzzed){
			this.setBorder(new LineBorder(Color.ORANGE, 3, true));
		}else{
			this.setBorder(null);
		}
		this.revalidate();
	}

	public Player getPlayer() {
		return p;
	}

	public void setLeader(boolean leader) {
		lblPlace.setFont(lblScore.getFont().deriveFont((float)scaler.calcY(defScoreFontSize)));
		lblPlace.setVisible(leader);
		this.revalidate();
	}
}
